<template>
    <el-form ref="defForm" :model="defForm" :rules="rules" label-width="80px">
        <el-form-item label="账号" prop="userName">
            <el-input v-model="defForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="password">
            <el-input v-model="defForm.password"></el-input>
        </el-form-item>
        <el-form-item size="large">
            <el-button type="primary" @click="$router.back()" >返回</el-button>
            <el-button type="primary" :loading="loading" @click="submitForm">保存</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    import token from "../../../token";
    import {update} from "../../../api/user";

    export default {
        name: "UpdatePwd",
        data() {
            return {
                loading: false,
                defForm: {
                    id: '',
                    userName: '',
                    password: '',
                },
                rules: {
                    userName:[{required: true, message: '必填项不能为空'}],
                    password:[{required: true, message: '必填项不能为空'}],
                }
            }
        },
        methods: {
            submitForm() {
                this.$refs['defForm'].validate(valid => {
                    if(valid) {
                        this.loading = true;
                        update(this.defForm).then(data => {
                            this.loading = false;
                            //将用户的最新信息写入localStorage
                            token.setUser(data.data)
                            this.defForm = data.data;
                            this.$message.success(data.msg);
                        }).catch(error => {
                            this.loading = false;
                            this.$message.error(error);
                        })
                    }
                })
            }
        }
    }
</script>

<style scoped>

</style>